﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.AdminControl.UserControls;
using System.Web.Services;
using RedHouse.Data.Entities;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using System.Web.Script.Serialization;

namespace RedHouse.AdminControl
{
    public partial class Category : System.Web.UI.Page
    {        
        public int Action { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Action"] != null)
                {
                    Action = int.Parse(Request.QueryString["Action"].ToString());
                }
                Initialize();
            }
        }

        private void Initialize()
        {
           
            switch (Action)
            {
                case 1:
                    CategoryDetail.Visible = true;
                    CategoryList.Visible = false;
                    break;
                case 2:
                    CategoryDetail.Visible = false;
                    CategoryList.Visible = true;
                    break;
                default:
                    break;
            }
        }
      

        [WebMethod()]
        public static string EditCategory(string id)
        {
            Guid categoryId = new Guid(id);
            ICategoryService categoryService = new CategoryService();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(categoryService.GetCategoryByID(categoryId));
        }

        [WebMethod()]
        public static void DeleteCategory(string id)
        {
            Guid categoryId = new Guid(id);
            ICategoryService categoryService = new CategoryService();
            categoryService.Delete(categoryId);
        }


        [WebMethod()]
        public static string SaveCategory(object category)
        {
            try
            {
                ICategoryService categoryService = new CategoryService();
                RedHouseCategory redHouseCategory = GetRedHouseCategory(category);
                categoryService.Save(redHouseCategory, false);
                return "save successfully!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static RedHouseCategory GetRedHouseCategory(object category)
        {
            RedHouseCategory retCategory = new RedHouseCategory();
            Dictionary<string, object> tmp = (Dictionary<string, object>)category;
            object id = null;
            object group = null;
            object lang = null;
            object des = null;
            object title = null;
            tmp.TryGetValue("Id", out id);
            tmp.TryGetValue("Group", out group);
            tmp.TryGetValue("LangId", out lang);
            tmp.TryGetValue("Description", out des);
            tmp.TryGetValue("Title", out title);

            retCategory.Title = title.ToString();
            retCategory.Id =new Guid( id.ToString());
            retCategory.Group = int.Parse(group.ToString());
            retCategory.LangId = new Guid(lang.ToString());
            retCategory.Description = des.ToString();
            retCategory.CreatedDate = DateTime.Now;
            retCategory.CreatedBy = "admin";
            
            return retCategory;
        }
    }
}